package app.caihan.wanandroid.ui.login

import android.os.Bundle
import androidx.lifecycle.Observer
import app.caihan.scframe.constant.DEBUG_TAG
import app.caihan.scframe.databind.DataBindingConfig
import app.caihan.scframe.ktx.getAppViewModel
import app.caihan.scframe.util.CoroutineState
import app.caihan.wanandroid.BR
import app.caihan.wanandroid.R
import app.caihan.wanandroid.base.WanBaseActivity
import app.caihan.wanandroid.base.event.AppViewModel
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.LogUtils
import org.jetbrains.anko.contentView

class LoginUi : WanBaseActivity<LoginUiModel>() {

    //Application全局的ViewModel，里面存放了一些账户信息，基本配置信息等
    private val appViewModel: AppViewModel by lazy { getAppViewModel<AppViewModel>() }

    override fun bindLayoutId(): Int {
        return R.layout.activity_login_ui
    }

    override fun initView(savedInstanceState: Bundle?) {
    }

    override fun createObserver() {
        mViewModel.apply {
            userInfo.observe(this@LoginUi, Observer {
                appViewModel.userInfo.postValue(it)
//                finish()
            })
        }
        appViewModel.run {
            userInfo.observeInActivity(this@LoginUi, Observer {
                LogUtils.dTag(
                    DEBUG_TAG,
                    "${javaClass.simpleName} :: observe appViewModel.userinfo = ${appViewModel.userInfo.value}"
                )
            })
            appColor.observeInActivity(this@LoginUi, Observer {
                LogUtils.dTag(
                    DEBUG_TAG,
                    "${javaClass.simpleName} :: observe appViewModel.appColor = $it"
                )
            })
        }
    }

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(BR.vm).addBindingParam(BR.click, ProxyClick())
    }

    override fun setStatusBarStyle() {
        contentView?.post {
            BarUtils.transparentStatusBar(this)
        }
    }

    override fun finish() {
        super.finish()
//        overridePendingTransition(
//            R.anim.activity_close_top_in,
//            R.anim.activity_close_bottom_out
//        )
    }

    override fun registerUiChange() {
        mViewModel.statusLiveData.observe(this, Observer {
            when (it.state) {
                CoroutineState.START.state -> showProgressDialog()
                CoroutineState.REFRESH.state -> dismissProgressDialog()
                CoroutineState.FINISH.state -> dismissProgressDialog()
                CoroutineState.ERROR.state -> dismissProgressDialog()
            }
        })
    }

    inner class ProxyClick {
        init {
            mViewModel.loginTitle.value = "去注册"
            mViewModel.loginTitle2.value = "登录"
        }

        fun back() {
            finish()
        }

        fun clearUserName() {
            mViewModel.username.value = ""
        }

        fun clearPwd() {
            mViewModel.password.value = ""
        }

        fun clearPwd2() {
            mViewModel.password2.value = ""
        }


        fun changeType() {
            when (mViewModel.type.value) {
                0 -> {
                    mViewModel.type.value = 1
                    mViewModel.loginTitle.value = "去登录"
                    mViewModel.loginTitle2.value = "注册"
                }
                1 -> {
                    mViewModel.type.value = 0
                    mViewModel.loginTitle.value = "去注册"
                    mViewModel.loginTitle2.value = "登录"
                }
            }
        }

        fun loginOrRegister() {
            appViewModel.appColor.value = (0..10).random()
//            when (mViewModel.type.value) {
//                0 -> {
//                    when {
//                        mViewModel.username.value.isEmpty() -> showMessage("请填写账号")
//                        mViewModel.password.value.isEmpty() -> showMessage("请填写密码")
//                        mViewModel.password.value.length < 6 -> showMessage("密码最少6位")
//                        else -> mViewModel.login()
//                    }
//                }
//                1 -> {
//                    when {
//                        mViewModel.username.value.isEmpty() -> showMessage("请填写账号")
//                        mViewModel.password.value.isEmpty() -> showMessage("请填写密码")
//                        mViewModel.password2.value.isEmpty() -> showMessage("请填写确认密码")
//                        mViewModel.password.value.length < 6 -> showMessage("密码最少6位")
//                        mViewModel.password.value != mViewModel.password2.value -> showMessage("密码不一致")
//                        else -> mViewModel.registerAndlogin()
//                    }
//                }
//            }
        }
    }

}